package com.igit.contact.action;

import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import com.gosophia.springsecurity.UserDetailsImpl;
import com.gosophiaigit.contact.busi.entity.Contact;
import com.gosophiaigit.contact.busi.entity.ContactGroup;
import com.gosophiaigit.contact.busi.service.IContactGroupService;
import com.gosophiaigit.contact.busi.service.IContactService;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 联系人组间操作Action
 * 
 * @anthor zhanghuanchao
 * @since 2011-3-25下午01:44:11
 */
@SuppressWarnings("serial")
@Controller("contactGroupBetweenAction")
@Scope("prototype")
public class ContactGroupBetweenAction extends ActionSupport {

	@Autowired
	private IContactGroupService contactGroupService;
	@Autowired
	private IContactService contactService;
	/** 联系人ID */
	private Long contactId;
	private String contactIds;
	/** 联系人组ID */
	private Long contactGroupId;
	/** 联系人源所在组ID */
	private Long sourceGroupId;
	/** 联系人目标所在组ID */
	private Long targetGroupId;
	/** 用来接收通过ID查询得到的联系人 */
	private Contact contact;
	/** 搜索字符串 */
	private String contactQuery;
	/** 查询类型：allContact\inContact\groupId */
	private String searchType;
	/** 搜索类型:queryType 01模糊查询02按首字母模糊查询 */
	private String queryType;
	/** 启动、禁用标识位：启用：1，禁用：2 */
	private Long stateId;
	/** 用来 接收查询的联系人列表数据 */
	private List<Contact> list;
	/** 用来接收下拉列表的组数据 */
	private Map<String, List<ContactGroup>> groupMap;

	/**
	 * 把联系人移到指定组中（移至）
	 */
	public String removeContactToGroup() {
		list = contactService
				.getContactByIds(getLongIds(contactIds));
		ContactGroup sourceGroup = contactGroupService
				.findContactGroupById(sourceGroupId);
		ContactGroup targetGroup = contactGroupService
				.findContactGroupById(targetGroupId);
		contactGroupService.romoveContactToGroup(sourceGroup, list,
				targetGroup);
		return null;
	}

	/**
	 * 添加联系人到指定组中（添加至）
	 */
	public String addContactToGroup() {
		list = contactService
				.getContactByIds(getLongIds(contactIds));
		ContactGroup contactGroup = contactGroupService
				.findContactGroupById(targetGroupId);
		contactGroupService.addContactToGroup(list, contactGroup);
		return null;
	}

	/**
	 * 把联系人从指定组中移除（移出）
	 */
	public String removeContactFromGroup() {
		list = contactService
				.getContactByIds(getLongIds(contactIds));
		ContactGroup contactGroup = contactGroupService
				.findContactGroupById(sourceGroupId);
		contactGroupService.removeContactFromGroup(list, contactGroup);
		return null;
	}

	/**
	 * 对指定联系人，启用、禁用标志 启用：1，禁用：2
	 */
	public String openOrCloseFlag() {
		// 此方法，会返回所操作后的标识位
		stateId = contactService.openOrCloseFlag(contactId);
		return SUCCESS;
	}

	/**
	 * 得到当前用户所在的个人组，和所有的公共组
	 */
	public String getAllContactGroupByCurrentUserId() {
		UserDetailsImpl userDetails = (UserDetailsImpl) SecurityContextHolder
				.getContext().getAuthentication().getPrincipal();
		// 得到当前用户
		Long currentUserId = userDetails.getUserAccountDTO().getUserInfoDTO()
				.getUserInfoId();
		Long userAccountId = userDetails.getUserAccountDTO().getUserAccountId();

		groupMap = contactGroupService
				.getAllContactGroupByCurrentUserId(currentUserId, userAccountId);

		return SUCCESS;
	}

	/**
	 * 点击某一联系人，得到联系人的详细信息
	 */
	public String getContactInfoByContactId(Long contactId) {
		contact = contactService.getContactById(contactId);
		return SUCCESS;
	}

	/**
	 * 按条件查询联系人列表 模糊查询、按拼音首字母查询
	 */
	public String getContactListByCondition() {
		UserDetailsImpl userDetails = (UserDetailsImpl) SecurityContextHolder
				.getContext().getAuthentication().getPrincipal();
		// 得到当前用户
		Long userAccountId = userDetails.getUserAccountDTO().getUserAccountId();
		if ("allContact".equals(searchType)) {
			// 查询所有的用户模糊查询方法String contactQuery,String queryType
			// queryType 01所有用户组普通模糊查询02所有用户组按首字母模糊查询
			list = contactGroupService.findUserInfoList(contactQuery
					.trim(), queryType, userAccountId);
		} else if ("inContact".equals(searchType)) {
			// 查询最近联系人下的用户模糊查询方法
			// queryType 01所有用户组普通模糊查询02所有用户组按首字母模糊查询
			// 得到当前用户
			Long userInfoId = userDetails.getUserAccountDTO().getUserInfoDTO()
					.getUserInfoId();
			list = contactGroupService.findContactRecordCountList(
					userInfoId, contactQuery.trim(), queryType, userAccountId);
		} else {
			// 根据组ID查询查询该组下的所有用户模糊查询方法Long contactGroupId,String
			// contactQuery,String queryType
			// queryType 01普通用户组模糊查询02普通用户组按首字母模糊查询
			contactGroupId = Long.parseLong(searchType);
			list = contactGroupService.findContactByContactGroupId(
					contactGroupId, contactQuery.trim(), queryType,
					userAccountId);
		}
		return SUCCESS;
	}

	private Long[] getLongIds(String contactIds) {
		Long[] longIds = null;
		if (contactIds != null && !"".equals(contactIds)) {
			String[] ids = contactIds.split(",");
			longIds = new Long[ids.length];
			for (int i = 0; i < ids.length; i++) {
				longIds[i] = Long.parseLong(ids[i]);
			}
		}
		return longIds;
	}

	public Long getContactId() {
		return contactId;
	}

	public void setContactId(Long contactId) {
		this.contactId = contactId;
	}

	public Long getContactGroupId() {
		return contactGroupId;
	}

	public void setContactGroupId(Long contactGroupId) {
		this.contactGroupId = contactGroupId;
	}

	public Long getSourceGroupId() {
		return sourceGroupId;
	}

	public void setSourceGroupId(Long sourceGroupId) {
		this.sourceGroupId = sourceGroupId;
	}

	public Long getTargetGroupId() {
		return targetGroupId;
	}

	public void setTargetGroupId(Long targetGroupId) {
		this.targetGroupId = targetGroupId;
	}

	public String getContactQuery() {
		return contactQuery;
	}

	public void setContactQuery(String contactQuery) {
		this.contactQuery = contactQuery;
	}

	public String getSearchType() {
		return searchType;
	}

	public void setSearchType(String searchType) {
		this.searchType = searchType;
	}

	public String getQueryType() {
		return queryType;
	}

	public void setQueryType(String queryType) {
		this.queryType = queryType;
	}

	public List<Contact> getList() {
		return list;
	}

	public void setList(List<Contact> list) {
		this.list = list;
	}

	public Long getStateId() {
		return stateId;
	}

	public void setStateId(Long stateId) {
		this.stateId = stateId;
	}

	public Contact getContact() {
		return contact;
	}

	public void setContact(Contact contact) {
		this.contact = contact;
	}

	public Map<String, List<ContactGroup>> getGroupMap() {
		return groupMap;
	}

	public void setGroupMap(Map<String, List<ContactGroup>> groupMap) {
		this.groupMap = groupMap;
	}

	public String getContactIds() {
		return contactIds;
	}

	public void setContactIds(String contactIds) {
		this.contactIds = contactIds;
	}

}
